package org.i_plus.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.i_plus.model.services.MapService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


@WebServlet("/MapPublishPic")
@MultipartConfig
public class MapPublishPic extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private MapService service;	
	public void init() throws ServletException {
		WebApplicationContext context =
				WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
		this.service = (MapService)context.getBean("mapService");
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
	
		
		//接收資料
		String account = request.getParameter("account");
		String content = request.getParameter("content");
		String temp3 = request.getParameter("x_axis");
		String temp4 = request.getParameter("y_axis");
		String title = request.getParameter("title");
		InputStream temp6 = request.getPart("pic").getInputStream();
		String submit = request.getParameter("submit");
		
		System.out.println(submit);
	
	
		//驗證資料
		Map<String , String> errors = new HashMap<String , String>();
		
		if(submit != null && submit.trim().length() != 0){
			if("發文".equals(submit)){
				if(account != null && account.trim().length() != 0){
					errors.put("account"  , "Please input acount for"+submit);
				}
			}
		}
		if(request.getPart("pic").getSize() != 0){
			String contentType = request.getPart("pic").getContentType();
			int checkimg = contentType.indexOf("image");
			if(checkimg == -1){
				errors.put("pic", "please input an image");
			}
		
		//轉換資料
			double x_axis = 0;
			if(temp3 != null && temp3.trim().length() !=0){
				try {
					x_axis = Double.parseDouble(temp3);
				} catch (NumberFormatException e) {
					errors.put("x_axis", "please input a number");
				}
			}
			double y_axis = 0;
			if(temp4 != null && temp4.trim().length() !=0){
				try {
					y_axis = Double.parseDouble(temp4);
				} catch (NumberFormatException e) {
					errors.put("y_axis", "please input a number");
				}
			}
			byte[] pic = new byte[(int)request.getPart("pic").getSize()];
			temp6.read(pic);
			
			if(errors != null && !errors.isEmpty()){
				request.getRequestDispatcher("/View/MapPublish.jsp").forward(request, response);
				return;
			}
			
			//呼叫model 依照model回傳結果 呼叫view	
		
			if("發佈".equals(submit)){

				BufferedOutputStream bos = null;
				try {
					File file = new File(request.getServletContext().getRealPath("/")+"images/map/"+title+".jpg");
					FileOutputStream os = new FileOutputStream(file);
					bos = new BufferedOutputStream(os);
					bos.write(pic);
				} catch (Exception e) {
					e.printStackTrace();
				} finally{
				    if(bos != null)
					bos.close();	
				}
				service.insert(account, content, x_axis, y_axis, title, null);
				System.out.println(request.getServletContext().getRealPath("/"));
				request.getRequestDispatcher("/View/MapPublish.jsp").forward(request , response);
				return;
			}
		}
	
	}
}	
